package com.bkhech.home.practice.lottery;

import com.bkhech.home.practice.lottery.dto.Box;
import com.bkhech.home.practice.lottery.dto.BoxAward;
import com.bkhech.home.practice.lottery.dto.UserAward;

import java.util.ArrayList;
import java.util.List;

/**
 * DrawAwardService
 * 抽奖逻辑
 *
 * @author guowm
 * @date 2024/06/26
 */
class DrawAwardTest {


    public static void initData(Box box) {
        box.setId(1);
        box.setStock(0);
        box.setBoxAwards(new ArrayList<>());

        final List<BoxAward> boxAwards = box.getBoxAwards();
        for (int i = 0; i < 5; i++) {
            final BoxAward boxAward = new BoxAward();
            boxAward.setBoxId(box.getId());
            boxAward.setStock(i + 1);
            boxAward.setGoodId(i + 1);
            boxAwards.add(boxAward);
        }

        box.setStock(boxAwards.stream().mapToInt(BoxAward::getStock).sum());
    }

    public static void main(String[] args) {
        // 抽取奖品的数量
        final int drawNum = 2;
        final Box box = new Box();
        initData(box);

        final DrawAwardService drawAwardService = new DrawAwardService();

        // 尝试抽奖3次，每次抽奖2个奖品，每次抽奖前都重置奖品库存和剩余件数
        for (int j = 0; j < 3; j++) {
            initData(box);

            final List<UserAward> userAwards = drawAwardService.drawAward(box, drawNum);
            userAwards.forEach(System.out::println);
            System.out.println("================");
        }
    }
}